package com.usermanage.controller;

import com.usermanage.SecurityConfig.SpringSecurityUtils;
import com.usermanage.Service.WechatService;
import com.usermanage.util.HttpUtil;
import com.usermanage.util.JsonData;
import com.usermanage.util.StringUtil;
import com.usermanage.util.WechatStatic;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/WechatApp")
public class WechatAppController {

    @Resource
    private WechatService wechatService;

    /**
     * 验证微信消息推送回调接口
     *
     * @param
     * @return
     * @throws IOException
     */
    @RequestMapping("/validMessageToken.json")
    public String validMessageToken(String signature, String timestamp, String nonce, String echostr) throws IOException {
        log.info("微信返回echostr：" + echostr);
        return echostr;
    }


    /**
     * 通过code获取用户信息是否与EOS账号存在绑定关系
     *
     * @param code
     * @return
     * @throws IOException
     */
    @RequestMapping("/validOpenId.json")
    public JsonData validOpenId(String code) throws IOException {
        log.info("微信请求成功，code：" + code);
        if (StringUtils.isNotBlank(code)) {
            String url = "https://api.weixin.qq.com/sns/jscode2session?";
            String param = "appid=" + WechatStatic.APPID + "&secret=" + WechatStatic.SECRET + "&grant_type=authorization_code" + "&js_code=" + code;
            String result = HttpUtil.getDemo(url + param);
            Map map = StringUtil.JsonToMap(result);
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            String sessionId = request.getSession().getId();
            map.put("sessionId", sessionId);
            boolean bool = wechatService.isExistFindByOpenId(String.valueOf(map.get("openid")));
            map.put("bound", bool);
            return JsonData.success(StringUtil.ObjectToJson(map));
        } else {
            return JsonData.fail("code不可以为空");
        }
    }

    /**
     * 绑定EOS账号
     *
     * @param request
     * @param account
     * @param password
     * @param openid
     * @return
     * @throws Exception
     */
    @RequestMapping("/boundEOS.json")
    public JsonData boundEOS(HttpServletRequest request, String account, String password, String openid) throws Exception {
        if (StringUtils.isBlank(account) || StringUtils.isBlank(password) || StringUtils.isBlank(openid)) {
            return JsonData.fail("参数不能为空");
        }
        return wechatService.boundEOS(request, account, password, openid);
//        if (bool) {
//            Map map = new HashMap();
//            map.put("company_id",SpringSecurityUtils.getCurrentUser().getCompany_id());
//            map.put("user_name",SpringSecurityUtils.getCurrentUser().getUsername());
//            return JsonData.success(map);
//        } else {
//            return JsonData.fail();
//        }
    }

    /**
     * 微信免登
     *
     * @param request
     * @param openid
     * @return
     * @throws Exception
     */
    @RequestMapping("/login.json")
    public JsonData boundEOS(HttpServletRequest request, String openid) throws Exception {
        if (StringUtils.isBlank(openid)) {
            return JsonData.fail("openid不能为空。");
        }
        return wechatService.login(request, openid);
//        if (bool) {
//            Map map = new HashMap();
//            map.put("company_id",SpringSecurityUtils.getCurrentUser().getCompany_id());
//            map.put("user_name",SpringSecurityUtils.getCurrentUser().getUsername());
//            return JsonData.success(map);
//        } else {
//            return JsonData.fail();
//        }
    }

    /**
     * 微信账号解绑
     *
     * @param request
     * @param openid
     * @return
     * @throws Exception
     */
    @RequestMapping("/unBind.json")
    public JsonData unBindEOS(HttpServletRequest request, String openid) throws Exception {
        if (StringUtils.isBlank(openid)) {
            return JsonData.fail("openid不能为空。");
        }
        return wechatService.unBindEos(openid);
    }

    /**
     * 获取左侧树列表
     *
     * @return
     * @throws IOException
     */
    @RequestMapping("/tree.json")
    public JsonData tree() throws IOException {
        return wechatService.tree();
    }

    /**
     * 根据分类ID获取SPU列表
     *
     * @param param
     * @return
     * @throws IOException
     */
    @RequestMapping("/spu.json")
    public JsonData spu(String param) throws IOException {
        return wechatService.spu(param);
    }

    /**
     * 根据SPUCode获取SKU列表
     *
     * @param page
     * @param limit
     * @param key
     * @param spuCode
     * @param cls
     * @return
     * @throws IOException
     */
    @RequestMapping("/skuList.json")
    public JsonData skuList(String page, String limit, String key, String spuCode, String cls) throws IOException {
        return wechatService.skuList(page, limit, key, spuCode, cls);
    }

    /**
     * 根据SKUCode获取SKU详情
     *
     * @param sku_code
     * @return
     */
    @RequestMapping("/skuInfo.json")
    public JsonData skuInfo(String sku_code) {
        return wechatService.skuInfo(sku_code);
    }

    /**
     * 微信通过点击消息免登跳转
     *
     * @param request
     * @param openid
     * @param url
     * @return
     * @throws Exception
     */
//    @RequestMapping("/message.json")
//    public JsonData gotoUrl(HttpServletRequest request, String openid, String url) throws Exception {
//        if (StringUtils.isBlank(openid)) {
//            return JsonData.fail("openid不能为空。");
//        }
//        boolean bool = wechatService.login(request, openid);
//        if (bool) {
//            Map map = new HashMap();
//            map.put("company_id", SpringSecurityUtils.getCurrentUser().getCompany_id());
//            map.put("url", url);
//            return JsonData.success(map);
//        } else {
//            return JsonData.fail();
//        }
//    }

}
